//
//  SuperviseReCheckDetailVC.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/1/10.
//
// 【首页-检查监督】检查任务-待复查-复查详情

import Foundation
import UIKit

class SuperviseReCheckDetailVC: SubLevelViewController {
    
    let taskV = SuperviseReCheckDetailView()
    
    var id: Int64!
    
    var datas: [SpRwbgtzModal?] = []
        
    init(id: Int64!) {
        super.init(nibName: nil, bundle: nil)
        self.id = id
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        title = "复查详情"
        navigationController?.navBarStyle(.white)
        view.backgroundColor = .bg
        
        setupUI()
        reloadData()
    }
    
    func reloadData() {
        API.getJcjdRwzgfcxq(withParam: SpDetailParam(id: id)) { responseModel in
            DispatchQueue.main.async {
                self.datas = responseModel.models ?? []
                self.taskV.reload(withModals: self.datas)
            }
        }
    }
    
    @objc func submitTapped() {
        if taskV.zgjls.count != self.datas.count {
            view.showToast(witMsg: "请完成复查")
            return
        }
        
        let param = RwzgfctjParam(id: id, zgjls: taskV.zgjls)
        do {
            let data: Data = try JSONEncoder().encode(param)
            let string = String(data: data, encoding: String.Encoding.utf8)
            API.postJcjdRwzgfctj(withParam: CommonJsonParam(jsonStr: string)) { responseModel in
                DispatchQueue.main.async {
                    if responseModel.errorCode == .Success {
                        NotificationCenter.default.post(name: NotiSupervise, object: nil)
                        self.navigationController?.popViewController(animated: true)
                    }
                }
            }
        } catch  {
            print("postJcjdCjrw error", error)
        }
    }
    
    // MARK: - Setup
    
    func setupUI() {
        taskV.submitBtn.addTarget(self, action: #selector(submitTapped), for: .touchUpInside)
        view.addSubview(taskV)
        taskV.snp.makeConstraints { make in
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
            make.left.right.equalToSuperview()
        }
    }
}
